import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

const auth = ['/notify']
router.beforeEach((to) => {
  // 给页面动态添加title
  document.title = `在线问诊-${to.meta.title || ''}`
  // 通过store获取token
  const store = useUserStore()
  if (!store.user.token && auth.includes(to.path)) {
    return '/login'
  }
})

export default router
